package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserDecoration;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserDecorationDaoImpl extends TempDataObjectDao<UserDecoration> {

    public Map<Integer, UserDecoration> getUserDecorationMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_decoration where userId=?";
        Map<Integer, UserDecoration> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDecoration data = parse(rs);
                dataMap.put(data.getDecorationId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public UserDecoration getUserDecorationById(long userId, int decorationId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_decoration where userId=? and decorationId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            call.setLong(2, decorationId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public Map<Long, Map<Integer, UserDecoration>> getNeedCheckUserDecorationMap() {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_decoration where endTime>0";
        Map<Long, Map<Integer, UserDecoration>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UserDecoration data = parse(rs);
                if(!dataMap.containsKey(data.getUserId())){
                    dataMap.put(data.getUserId(), new ConcurrentHashMap<>());
                }
                dataMap.get(data.getUserId()).put(data.getDecorationId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    @Override
    protected boolean addData(UserDecoration data) {
        PreparedStatement call = null;
        String sql = "insert into t_u_decoration(userId,decorationId,endTime,modifyTime) values (?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDecorationId());
            call.setLong(i++, data.getEndTime());
            call.setLong(i++, data.getModifyTime());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserDecoration data) {
        PreparedStatement call = null;
        String sql = "update t_u_decoration set endTime=?,modifyTime=? where userId=? and decorationId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getEndTime());
            call.setLong(i++, data.getModifyTime());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getDecorationId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserDecoration parseData(ResultSet rs) throws SQLException {
        UserDecoration data = new UserDecoration();
        data.setUserId(rs.getLong("userId"));
        data.setDecorationId(rs.getInt("decorationId"));
        data.setEndTime(rs.getLong("endTime"));
        data.setModifyTime(rs.getLong("modifyTime"));
        return data;
    }
}
